package com.seleniti.enpareja.controller.ui.view;

import org.ioc.commons.flowcontrol.actioncontroller.ActionController;
import org.ioc.commons.flowcontrol.eventbus.EventBus;
import org.ioc.commons.flowcontrol.eventbus.EventHandler;
import org.ioc.commons.flowcontrol.logincontroller.IsLoggedUserInfoConsumer;
import org.ioc.commons.flowcontrol.operationmanager.OperationManager;
import org.ioc.commons.model.globaldata.GlobalDataController.Consumer;
import org.ioc.commons.model.globaldata.IsGlobalDataConsumer;
import org.ioc.commons.ui.features.HasInfoType.InfoType;
import org.ioc.commons.utils.FormatterLogger;

import com.seleniti.enpareja.controller.common.GlobalData;
import com.seleniti.enpareja.controller.common.domain.LoggedUserInfo;
import com.seleniti.enpareja.controller.ui.base.EnParejaViewBasePresenter;
import com.seleniti.enpareja.controller.ui.view.UserSettingsView.Action;
import com.seleniti.enpareja.controller.ui.view.UserSettingsView.Event;
import com.seleniti.enpareja.controller.ui.view.UserSettingsView.Operation;
import com.seleniti.enpareja.controller.ui.widget.UserForm;

/**
 * Presenter for the SignUp view
 * 
 * @author Jesús Lunar Pérez
 * 
 */
public class UserSettingsViewPresenter extends
		EnParejaViewBasePresenter<UserSettingsView, UserSettingsView.Event, UserSettingsView.Operation, UserSettingsView.Action>
		implements UserSettingsView.Presenter, IsLoggedUserInfoConsumer<LoggedUserInfo>, IsGlobalDataConsumer<GlobalData> {

	private static final FormatterLogger logger = FormatterLogger.getLogger(UserSettingsViewPresenter.class);

	private Consumer<GlobalData> consumer;

	public UserSettingsViewPresenter(UserSettingsView display, EventBus<Event> eventbus,
			OperationManager<Operation> operationManager, ActionController<Action> actionController) {
		super(display, eventbus, operationManager, actionController);
		this.consumer = this.globalDataController.registerGlobalDataConsumer(this);
		this.consumer.requires(GlobalData.UserAgent);
	}

	@Override
	public void onInitDisplay() {
		super.onInitDisplay();
		logger.entering("onInitDisplay");

		display.userForm().caption().setText("Tus datos:");
		display.userForm().saveActionText().setText("Guardar tus datos");

		logger.exiting("onInitDisplay");
	}

	protected void bindComponents() {
		super.bindComponents();
		display.userForm().eventBus().bindEvent(UserForm.Event.UserSaved, new EventHandler<UserForm.Event>() {

			@Override
			public void handleEvent(Object sender, com.seleniti.enpareja.controller.ui.widget.UserForm.Event event,
					Object... params) {
				// String userEmail = (String) params[0];
				// placeController.goTo(EnParejaPlace.LoginPlace,true,
				// EnParejaPlace.Main, userEmail);
				logger.info("Refresing logged user info...");
				// boolean refreshing =
				// globalDataController.refreshGlobalData(GlobalData.LoggedUserInfo);
				loginController.refreshLoggedUserInfo();
				// logger.info("... refreshing? {0} status? {1}", refreshing,
				// globalDataController.getGlobalDataStatus(GlobalData.LoggedUserInfo));

				display.userForm().extraInfo().setType(InfoType.OPERATION_OK);
				display.userForm().extraInfo().setText("Datos guardados correctamente");

			}
		});
	}

	@Override
	public void onActive() {
		super.onActive();

		shell.refreshButtonVisibility().setVisible(false);

		this.loginController.requiresLoggedUserInfoIfDisplayActive(this);
		this.consumer.ensureRequirements();
	}

	@Override
	public void onRequiredGlobalDataReady() {
		showGoBackButtonIfRequired();
	}

	@Override
	public void onRequiredGlobalDataNotReadyYet() {
		/*
		 * Nothing to do
		 */
	}
	
	@Override
	public void onRequiredGlobalDataUpdated(GlobalData globalDataUpdated) {
		showGoBackButtonIfRequired();
	}

	@Override
	public void onLoggedUserInfoDataReady() {
		refreshData();
	}

	@Override
	protected void refreshData() {
		super.refreshData();

		refreshLoggedUserInfo();
	}

	protected void refreshLoggedUserInfo() {
		LoggedUserInfo lui = this.loginController.getLoggedUserInfo();

		display.userForm().editUser(lui.getUserId());

		display.userForm().downloadApp()
				.setVisible(display.isShowDownloadAppWhenPassword() && lui != null && lui.isPasswordSet());
	}

	@Override
	public void onLoggedUserInfoDataUpdated(LoggedUserInfo loggedUserInfo) {
		refreshLoggedUserInfo();
	}

	@Override
	public void onLoggedUserInfoNotReadyYet() {
	}

}
